package com.aaa.vip.service.impl;

import com.aaa.common.entity.UserBalance;
import com.aaa.common.entity.UserBalanceLog;
import com.aaa.common.entity.UserDetail;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;
import com.aaa.common.vo.StoreIncomeStatisticsVO;
import com.aaa.vip.dao.UserBalanceDao;
import com.aaa.vip.dao.UserBalanceLogDao;
import com.aaa.vip.dao.UserDetailDao;
import com.aaa.vip.service.UserDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserDetail)表服务实现类
 *
 * @author makejava
 * @since 2025-09-22 11:19:59
 */
@Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailService {
    @Resource
    private UserDetailDao userDetailDao;
    @Resource
    private UserBalanceLogDao userBalanceLogDao;
    @Resource
    private UserBalanceDao userBalanceDao;

    @Override
    public List<StoreIncomeStatisticsVO> getStoreDailyIncome(String storeName, String monthFormat) {
        // 调用DAO层查询指定商家在指定月份的每日进账金额
        return userDetailDao.selectStoreDailyIncome(storeName, monthFormat);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param detailId 主键
     * @return 实例对象
     */
    @Override
    public UserDetail queryById(String detailId) {
        return this.userDetailDao.queryById(detailId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<UserDetail> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        System.out.println("page================" + page);
        //获取查询对象
        UserDetail userDetail = page.getData();
        return new PageInfo(userDetailDao.queryAllByLimit(userDetail));
    }

    /**
     * 新增数据
     *
     * @param userDetail 实例对象
     * @return 实例对象
     */
    @Override
    public UserDetail insert(UserDetail userDetail) {
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(userDetail.getUserId());
        userBalanceLog.setStoreId(userDetail.getStoreId());
        userBalanceLog.setExpenditure(userDetail.getExpenditure());
        userBalanceLog.setRechargeMoney(userDetail.getRechargeMoney());
        userBalanceLog.setDiscountsMoney(userDetail.getDiscountsMoney());
        userBalanceLog.setAccountMoney(userDetail.getAccountMoney());

        UserBalance userBalance = new UserBalance();
        userBalance.setUserId(userDetail.getUserId());
        userBalance.setStoreId(userDetail.getStoreId());
        userBalance.setAmount(userDetail.getAccountMoney());
        userBalanceDao.insertNew(userBalance);
        userBalanceLogDao.insert(userBalanceLog);

        this.userDetailDao.insert(userDetail);
        return userDetail;
    }

    /**
     * 修改数据
     *
     * @param userDetail 实例对象
     * @return 实例对象
     */
    @Override
    public UserDetail update(UserDetail userDetail) {
        this.userDetailDao.update(userDetail);
        return this.queryById(userDetail.getDetailId());
    }

    /**
     * 通过主键删除数据
     *
     * @param detailId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String detailId) {
        return this.userDetailDao.deleteById(detailId) > 0;
    }
}
